<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="标题" prop="title"
                :rules="[
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="图片" prop="image"
                :rules="[
                 ]">
              <el-upload ref="image"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/porganrecruit/pOrganRecruit`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url);
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="imageArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="内容" prop="content"
                :rules="[
                 ]">
            <WangEditor ref="contentEditor" v-model="inputForm.content"/>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="附件路径" prop="filePath"
                :rules="[
                 ]">
              <el-upload ref="filePath"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/porganrecruit/pOrganRecruit`"
                    :headers="{token: $cookie.get('token')}"
                    :before-upload="handleAreve2"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.filePath = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.filePath = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="filePathArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发布人" prop="publisher"
                :rules="[
                 ]">
              <el-input v-model="inputForm.publisher" placeholder="请填写发布人"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        imageArra: [],
        filePathArra: [],
        inputForm: {
          id: '',
          title: '',
          image: '',
          content: '',
          filePath: '',
          publisher: '',
          deleteFlag: '0',
          remarks:'',
        }
      }
    },
    components: {
      WangEditor
    },
    methods: {
      showImg(url) {
        this.$hevueImgPreview(url);
      },
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建机关招聘`
        } else if (method === 'edit') {
          this.title = '修改机关招聘'
        } else if (method === 'view') {
          this.title = '查看机关招聘'
        }
        this.imageArra = []
        this.filePathArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.contentEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/porganrecruit/pOrganRecruit/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.pOrganRecruit)
              this.$refs.contentEditor.init(this.inputForm.content)
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.filePath.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.filePathArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      handleAreve2(file) {
        var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
        const isLt500k = file.size / 1024 / 1024 < 20;
        const extension = testmsg === "pdf" || testmsg === "jpg" || testmsg === "png"|| testmsg === "JPG" || testmsg === "PNG";//限制只能上传pdf文件，否则阻止上传
        if (testmsg === "pdf"){
            this.remarks = 'pdf'
        } 
        if (testmsg === "jpg" ||  testmsg === "JPG"){
            this.remarks = 'jpg'
        } 
        if (testmsg === "png" ||  testmsg === "PNG"){
            this.remarks = 'png'
        } 
        if (!extension ) {
          this.$message({
            message: "上传文件只能是pdf文件或者jpg/png图片格式!",
            type: "warning",
          });
        }
        if (!isLt500k) {
          this.$message.error("上传文件大小不能超过20M!");
          return false;
        }
        this.inputForm.remarks = testmsg;
        return extension;
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/porganrecruit/pOrganRecruit/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>